/*
 * Copyright 2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import * as _ from "lodash"
import { PageList } from "../data"
import httpClient from "../httpclient"

export class Store {
  public id: string
  public industry: string
  public name: string
  public logo: string
  public description: string
  public country_code: string
  public mobile: string
  public zip: string
  private province_id: string
  public province
  private city_id: string
  public city
  private county_id: string
  public county
  public address: string
  private created_time: Date

  public get countryCode() {
    return this.country_code
  }

  public set countryCode(countryCode) {
    this.country_code = countryCode
  }

  public get provinceId() {
    return this.province_id
  }

  public set provinceId(provinceId) {
    this.province_id = provinceId
  }

  public get cityId() {
    return this.city_id
  }

  public set cityId(cityId) {
    this.city_id = cityId
  }

  public get countyId() {
    return this.county_id
  }

  public set countyId(countyId) {
    this.county_id = countyId
  }

  public get createdTime() {
    return this.created_time
  }

  public set createTime(createdTime: Date) {
    this.created_time = createdTime
  }

  public static empty() {
    return new Store()
  }
}

class StoreQuery {

  private params = {}

  public page(page: number) {
    this.params["page"] = page
    return this
  }

  public limit(limit: number) {
    this.params["limit"] = limit
    return this
  }

  public ownerId(value: string) {
    this.params["owner_id"] = value
    return this
  }

  public list(): Promise<PageList<Store>> {
    return httpClient.get(`/v1/stores`, { params: this.params })
      .then(({ data }) => {
        return _.assign(PageList.empty(), data, {
          elements: _.map(data.elements, element => _.assign(new Store(), element)),
        })
      })
  }
}

export class StoreServiceV1 {

  public static createStore(store: Store): Promise<void> {
    return httpClient.post("/v1/stores", store)
  }

  public static cancelStore(storeId: string): Promise<void> {
    return httpClient.delete(`/v1/stores/${storeId}`)
  }

  public static saveStore(store: Store): Promise<void> {
    return httpClient.put(`/v1/stores/${store.id}`, store)
  }

  public static getStore(storeId: string): Promise<Store> {
    return httpClient.get(`/v1/stores/${storeId}`)
      .then(({ data }) => _.assign(new Store(), data))
  }

  public static existsStore(storeId: string): Promise<boolean> {
    return httpClient.get(`/v1/stores/${storeId}/exists`).then(({ data }) => data)
  }

  public static getStores(): StoreQuery {
    return new StoreQuery()
  }
}
